﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Waaahsabi.Controls.TheGame
{
	[TemplateVisualState(GroupName = "Default", Name = "Welcome")]
	[TemplateVisualState(GroupName = "Default", Name = "SecurityList")]
	[TemplateVisualState(GroupName = "Default", Name = "FeatureList")]
	[TemplateVisualState(GroupName = "Default", Name = "Subsystem")]
	public abstract class GenericSystemInterface : Control
	{
		#region SystemName DependencyProperty
		public static readonly DependencyProperty SystemNameProperty =
		DependencyProperty.Register
			(
			"SystemName",
			typeof(string),
			typeof(GenericSystemInterface),
			new PropertyMetadata("Dummy System (Dummy Corp.)")
			);

		public string SystemName
		{
			get { return (string)GetValue(SystemNameProperty); }
			set { SetValue(SystemNameProperty, value); }
		}
		#endregion

		#region SystemWelcomeMessage DependencyProperty
		public static readonly DependencyProperty SystemWelcomeMessageProperty =
		DependencyProperty.Register
			(
			"SystemWelcomeMessage",
			typeof(string),
			typeof(GenericSystemInterface),
			new PropertyMetadata("Welcome on the Dummy System of Dummy Corp. By accessing this system you agree to the standard Terms Of Usage of Dummy Corp. Violations will be strictly prosecuted. All access and activity is being logged.")
			);

		public string SystemWelcomeMessage
		{
			get { return (string)GetValue(SystemWelcomeMessageProperty); }
			set { SetValue(SystemWelcomeMessageProperty, value); }
		}
		#endregion

		#region SecuritySystems DependencyProperty
		public static readonly DependencyProperty SecuritySystemsProperty =
		DependencyProperty.Register
			(
			"SecuritySystems",
			typeof(ObservableCollection<GenericSelectorButton>),
			typeof(GenericSystemInterface),
			new PropertyMetadata
				(
				null //new ObservableCollection<GenericSelectorButton>()
				)
			);

		public ObservableCollection<GenericSelectorButton> SecuritySystems
		{
			get { return (ObservableCollection<GenericSelectorButton>)GetValue(SecuritySystemsProperty); }
			set { SetValue(SecuritySystemsProperty, value); }
		}
		#endregion

		#region SystemFeatures DependencyProperty
		public static readonly DependencyProperty SystemFeaturesProperty =
		DependencyProperty.Register
			(
			"SystemFeatures",
			typeof(ObservableCollection<GenericSelectorButton>),
			typeof(GenericSystemInterface),
			new PropertyMetadata(null)
			);

		public ObservableCollection<GenericSelectorButton> SystemFeatures
		{
			get { return (ObservableCollection<GenericSelectorButton>)GetValue(SystemFeaturesProperty); }
			set { SetValue(SystemFeaturesProperty, value); }
		}
		#endregion

		#region SubSystemContent DependencyProperty
		public static readonly DependencyProperty SubSystemContentProperty =
		DependencyProperty.Register
			(
			"SubSystemContent",
			typeof(UIElement),
			typeof(GenericSystemInterface),
			new PropertyMetadata(null)
			);

		public UIElement SubSystemContent
		{
			get { return (UIElement)GetValue(SubSystemContentProperty); }
			set { SetValue(SubSystemContentProperty, value); }
		}
		#endregion

		#region SystemId DependencyProperty
		public static readonly DependencyProperty SystemIdProperty =
		DependencyProperty.Register
			(
			"SystemId",
			typeof(Guid),
			typeof(GenericSystemInterface),
			new PropertyMetadata(null)
			);

		public Guid SystemId
		{
			get { return (Guid)GetValue(SystemIdProperty); }
			set { SetValue(SystemIdProperty, value); }
		}
		#endregion

		#region ConnectionId DependencyProperty
		public static readonly DependencyProperty ConnectionIdProperty =
		DependencyProperty.Register
			(
			"ConnectionId",
			typeof(Guid),
			typeof(GenericSystemInterface),
			new PropertyMetadata(Guid.Empty)
			);

		public Guid ConnectionId
		{
			get { return (Guid)GetValue(ConnectionIdProperty); }
			set { SetValue(ConnectionIdProperty, value); }
		}
		#endregion

		public event RoutedEventHandler WelcomeContinueClick;
		public event RoutedEventHandler WelcomeDisconnectClick;

		public event EventHandler<FeatureEventArgs> SecuritySystemClick;
		public event EventHandler<FeatureEventArgs> SystemFeatureClick;

		private GenericWelcomeScreen _welcomeScreen;
		private GenericFeatureList _securitySystemList;
		private GenericFeatureList _systemFeatureList;
		private GenericSubSystemInterface _subSystemInterface;
		private Button _btnBack;

		private LastListType _lastListType;

		protected GenericSystemInterface()
		{
			DefaultStyleKey = typeof(GenericSystemInterface);
			SystemFeatures = new ObservableCollection<GenericSelectorButton>();
			//SystemFeatures.CollectionChanged += SystemFeatures_CollectionChanged;
			SecuritySystems = new ObservableCollection<GenericSelectorButton>();
			//SecuritySystems.CollectionChanged += SecuritySystems_CollectionChanged;
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_welcomeScreen = GetTemplateChild("welcomeScreen") as GenericWelcomeScreen;
			if (_welcomeScreen != null)
			{
				_welcomeScreen.ContinueClicked += WelcomeContinueClick;
				_welcomeScreen.DisconnectClicked += WelcomeDisconnectClick;
			}

			_securitySystemList = GetTemplateChild("securitySystemList") as GenericFeatureList;
			if(_securitySystemList != null)
			{
				_securitySystemList.ItemClicked += (s, e) =>
				{
					if(SecuritySystemClick != null)
						SecuritySystemClick(this, new FeatureEventArgs { InterfaceId = e.InterfaceId, FeatureName = e.FeatureName });
				};
			}

			_systemFeatureList = GetTemplateChild("systemFeatureList") as GenericFeatureList;
			if(_systemFeatureList != null)
			{
				_systemFeatureList.ItemClicked += (s, e) =>
				{
					if(SystemFeatureClick != null)
						SystemFeatureClick(this, new FeatureEventArgs {InterfaceId = e.InterfaceId, FeatureName = e.FeatureName });
				};
			}

			_btnBack = GetTemplateChild("btnBack") as Button;
			if(_btnBack != null)
			{
				_btnBack.Click += (s, e) =>
				{
					switch (_lastListType)
					{
						case LastListType.Security:
							ShowSecsysList();
							break;
						case LastListType.Feature:
							ShowFeatureList();
							break;
					}
				};
			}
			_subSystemInterface = GetTemplateChild("subSystemInterface") as GenericSubSystemInterface;
			if(_subSystemInterface != null)
			{
				
			}
		}

		public void AddSecuritySystem(string name, ulong interfaceId, ulong featureId)
		{
			GenericSelectorButton btn = new GenericSelectorButton {ItemInterfaceId = interfaceId, ItemName = name, FeatureId = featureId};
			btn.Click += (s, e) =>
			{
				if (SecuritySystemClick != null)
					SecuritySystemClick(this, new FeatureEventArgs {InterfaceId = btn.ItemInterfaceId, FeatureName = btn.ItemName, FeatureId = btn.FeatureId});
			};
			SecuritySystems.Add(btn);
		}

		public void RemoveSecuritySystem(ulong id)
		{
			GenericSelectorButton btn = SecuritySystems.Where(it => it.ItemInterfaceId == id).FirstOrDefault();
			if (btn != null)
				SecuritySystems.Remove(btn);
		}

		public void AddSystemFeature(string name, ulong interfaceId, ulong featureId)
		{
			GenericSelectorButton btn = new GenericSelectorButton { ItemInterfaceId = interfaceId, ItemName = name, FeatureId = featureId};
			btn.Click += (s, e) =>
			{
				if (SystemFeatureClick != null)
					SystemFeatureClick(this, new FeatureEventArgs {InterfaceId = btn.ItemInterfaceId, FeatureName = btn.ItemName, FeatureId = btn.FeatureId});
			};
			SystemFeatures.Add(btn);
		}

		public void RemoveSystemFeature(ulong id)
		{
			GenericSelectorButton btn = SystemFeatures.Where(it => it.ItemInterfaceId == id).FirstOrDefault();
			if (btn != null)
				SystemFeatures.Remove(btn);
		}

		public void ShowWelcomeScreen()
		{
			VisualStateManager.GoToState(this, "Welcome", true);
		}

		public void ShowSecsysList()
		{
			VisualStateManager.GoToState(this, "SecurityList", true);
			_lastListType = LastListType.Security;
		}

		public void ShowFeatureList()
		{
			VisualStateManager.GoToState(this, "FeatureList", true);
			_lastListType = LastListType.Feature;
		}

		public void ShowSubSystem(UIElement contentElement)
		{
			VisualStateManager.GoToState(this, "Subsystem", true);
		}
	}

	public enum LastListType
	{
		Security,
		Feature
	}
}
